package com.macro.mall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.bo.AdminUserDetails;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.ResultCode;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.common.utils.Assist;
import com.macro.mall.mapper.RcRegistrationContentMapper;
import com.macro.mall.model.RcRegistrationContent;
import com.macro.mall.model.UmsAdmin;
import com.macro.mall.model.UmsRole;
import com.macro.mall.service.RcRegistrationContentService;
import com.macro.mall.service.UmsAdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * RcRegistrationContent的服务接口的实现类
 *
 * @author gaodx
 */
@Slf4j
@Service
public class RcRegistrationContentServiceImpl implements RcRegistrationContentService {

    private boolean isDebugEnabled;

    @Autowired
    private RcRegistrationContentMapper rcRegistrationContentMapper;
    // TODO 当你看到这个方法时你应该创建一个工具类做通用的方法,定义自己的返回格式化

    @Autowired
    private UmsAdminService adminService;

    @Override
    public List<RcRegistrationContent> find(RcRegistrationContent value, Integer pageNum, Integer pageSize, String taskName) {
        //TODO这里可以做通过Assist做添加查询
        List<RcRegistrationContent> result = null;
        Assist assist = new Assist();

        boolean flag = true;
        try {
            if (!"task".equals(taskName)) {
                SecurityContext ctx = SecurityContextHolder.getContext();
                Authentication auth = ctx.getAuthentication();
                AdminUserDetails memberDetails = (AdminUserDetails) auth.getPrincipal();
                UmsAdmin umsAdmin = memberDetails.getUmsAdmin();
                Long id = umsAdmin.getId();
                List<UmsRole> roleList = adminService.getRoleList(id);
                for (UmsRole one : roleList) {
                    if (one.getName().equals("超级管理员")) {
                        flag = false;
                    }
                }
                if (flag) {
                    assist.andEq("adminId", id);
                }
            }
            if (!StrUtil.isBlankIfStr(value.getStatus())) {
                assist.andEq("status", value.getStatus());
            }
            if (!StrUtil.isBlankIfStr(value.getClassified())) {
                assist.andEq("classified", value.getClassified());
            }
            if (!StrUtil.isBlankIfStr(value.getTitle())) {
                assist.andLike("title", "%" + value.getTitle() + "%");
            }
            if (!StrUtil.isBlankIfStr(value.getIsRecommend())) {
                assist.andEq("is_Recommend", value.getIsRecommend());
            }
            PageHelper.startPage(pageNum, pageSize);
            PageHelper.orderBy("create_time desc");
            result = rcRegistrationContentMapper.selectRcRegistrationContent(assist);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行获取RcRegistrationContent数据集-->结果:", result);
        }
        return result;
    }

    @Override
    public RcRegistrationContent findOne(Integer id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过RcRegistrationContent的id获得RcRegistrationContent对象-->失败:id不能为空");
            }
            Asserts.fail("执行通过RcRegistrationContent的id获得RcRegistrationContent对象-->失败:id不能为空");
        }
        RcRegistrationContent result = null;
        try {
            result = rcRegistrationContentMapper.selectRcRegistrationContentById(id);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过RcRegistrationContent的id获得RcRegistrationContent对象-->结果:", result);
        }
        return result;
    }

    @Override
    public CommonResult saveNotNull(RcRegistrationContent value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行将RcRegistrationContent中属性值不为null的数据保存到数据库-->失败:对象不能为空");
            }
            Asserts.fail("失败:对象不能为空");
        }
        int result = 0;
        try {
            SecurityContext ctx = SecurityContextHolder.getContext();
            Authentication auth = ctx.getAuthentication();
            AdminUserDetails memberDetails = (AdminUserDetails) auth.getPrincipal();
            UmsAdmin umsAdmin = memberDetails.getUmsAdmin();
            value.setAdminId(umsAdmin.getId() + "");
            if (!StrUtil.isBlankIfStr(value.getEtime()) && !StrUtil.isBlankIfStr(value.getStime())) {
                if (value.getEtime().getTime() < value.getStime().getTime()) {
                    Asserts.fail("开始时间应晚于结束时间");
                }
                value.setFlag(getFlag(value));
            }
            result = rcRegistrationContentMapper.insertNotNullRcRegistrationContent(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行将RcRegistrationContent中属性值不为null的数据保存到数据库-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult updateNotNullById(RcRegistrationContent value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行通过RcRegistrationContent的id更新RcRegistrationContent中属性不为null的数据-->失败:对象为null");
            }
            Asserts.fail("执行通过RcRegistrationContent的id更新RcRegistrationContent中属性不为null的数据-->失败:对象为null");
        }
        int result = 0;
        try {
            if (!StrUtil.isBlankIfStr(value.getEtime()) && !StrUtil.isBlankIfStr(value.getStime())) {
                if (value.getEtime().getTime() < value.getStime().getTime()) {
                    Asserts.fail("开始时间应晚于结束时间");
                }
                value.setFlag(getFlag(value));
            }
            result = rcRegistrationContentMapper.updateNotNullRcRegistrationContentById(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过RcRegistrationContent的id更新RcRegistrationContent中属性不为null的数据-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult deleteById(Integer id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过RcRegistrationContent的id删除RcRegistrationContent-->失败:id不能为空");
            }
            Asserts.fail("执行通过RcRegistrationContent的id删除RcRegistrationContent-->失败:id不能为空");
        }
        int result = 0;
        try {
            result = rcRegistrationContentMapper.deleteRcRegistrationContentById(id);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过RcRegistrationContent的id删除RcRegistrationContent-->结果:", result);
        }
        return CommonResult.success(null);
    }

    public String getFlag(RcRegistrationContent one) {
        long l = System.currentTimeMillis();
        //志愿活动状态 1-活动未开始 2-活动结束 3-报名未开始 4-活动报名中 5-已报名 6-名额已满
        String flag = one.getFlag();
        if (one.getSRegistration() != null && one.getSRegistration().getTime() > l) {
            return "3";
        } else if ((one.getSRegistration() != null && one.getERegistration() != null) && one.getSRegistration().getTime() < l && one.getERegistration().getTime() > l) {
            return "4";
        } else if (one.getStime().getTime() > l) {
            return "1";
        } else if (one.getEtime().getTime() < l) {
            return "2";
        }
        return "1";
    }


}